﻿#include "qwidget_dl_para1.h"



QWidgetDlPara1::QWidgetDlPara1(QWidget *parent,QString tableName)
    : QWidget(parent)
{
    m_qtableview = new QTableView(this);
    QSqlQuery sql_query;
    sql_query.exec(QString("select count(*) from sqlite_master where type='table' and name='%1'").arg(tableName));
    if(sql_query.next())
    {
         if(sql_query.value(0).toInt()==0)
         {
             sql_query.exec(QString("create table '%1' (id int primary key,classify TEXT,score TEXT,pixel TEXT)").arg(tableName));
         }
    }

    m_qsltablemodel = new QSqlTableModel();
    m_qsltablemodel->setTable(tableName);
//    int column_count = m_qsltablemodel->columnCount();
//    if(column_count ==12)
//    {
//            sql_query.exec(QString("ALTER TABLE '%1' ADD COLUMN distance_value_state INT").arg(tableName));
//            sql_query.exec(QString("ALTER TABLE '%1' ADD COLUMN distance_value INT").arg(tableName));
//            m_qsltablemodel->setTable(tableName);
//    }

    m_qsltablemodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_qsltablemodel->select();

    m_qsltablemodel->setHeaderData(0, Qt::Horizontal, ("Id"));
    m_qsltablemodel->setHeaderData(1, Qt::Horizontal, ("Classify"));
    m_qsltablemodel->setHeaderData(2, Qt::Horizontal, ("Score(0-1)"));
    m_qsltablemodel->setHeaderData(3, Qt::Horizontal, ("Pixel"));
    m_qtableview->setModel(m_qsltablemodel);
    m_qtableview->resizeColumnsToContents();

    m_qtableview->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_qtableview->horizontalHeader()->setFixedHeight(40);				// 设置表头的高度
    m_qtableview->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); // 设置表头背景色


    //    m_qtableview->setSelectionMode(QAbstractItemView::SingleSelection);//设置单选
    m_qtableview->setEditTriggers(QAbstractItemView::DoubleClicked);
    m_qtableview->setStyleSheet("selection-background-color:lightblue;");	// 设置选中背景色


    m_qmenu = new QMenu();
    m_qaction_delect = new QAction("delect row");
    m_qaction_insert = new QAction("insert row");
    m_qaction_revert = new QAction("undo");
    m_qaction_submit = new QAction("confirm");

    m_qmenu->addAction(m_qaction_delect);
    m_qmenu->addAction(m_qaction_insert);
    m_qmenu->addAction(m_qaction_revert);
    m_qmenu->addAction(m_qaction_submit);

    connect(m_qaction_insert, SIGNAL(triggered(bool)), this, SLOT(insertRow()));
    connect(m_qaction_delect, SIGNAL(triggered(bool)), this, SLOT(deleteRow()));
    connect(m_qaction_revert, SIGNAL(triggered(bool)), this, SLOT(revert()));
    connect(m_qaction_submit, SIGNAL(triggered(bool)), this, SLOT(submit()));

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(m_qtableview);
    setLayout(mainLayout);
}

void QWidgetDlPara1::contextMenuEvent(QContextMenuEvent *event)
{
    Q_UNUSED(event);
    if(m_qtableview->hasFocus())
    {
        m_qmenu->move(cursor().pos());
        m_qmenu->show();
    }
}

void QWidgetDlPara1::deleteRow()
{
    int select_row = m_qtableview->currentIndex().row();
    bool state = m_qsltablemodel->removeRow(select_row);
    if(!state)
    {
        QMessageBox::critical(this,"QWidgetDlPara1","delete ng");
    }
}

void QWidgetDlPara1::insertRow()
{
    int select_row_add_1 = m_qtableview->currentIndex().row()+1;
    bool state = m_qsltablemodel->insertRow(select_row_add_1);
    if(!state)
    {
        QMessageBox::critical(this,"QWidgetDlPara1","insert ng");
    }
}

void QWidgetDlPara1::submit()
{
    m_qsltablemodel->database().transaction();
    //QSqlDatabase::transaction() 方法用于启动一个新的数据库事务。事务是一组作为单个逻辑工作单元执行的操作，这些操作要么全都成功，要么在遇到错误时全都回滚（撤销）。使用事务可以确保数据的一致性和完整性。
    if (m_qsltablemodel->submitAll()) {
        m_qsltablemodel->database().commit();
    } else {
        m_qsltablemodel->database().rollback();
        QMessageBox::warning(this, tr("QWidgetDlPara1"),
                             tr("The database reported an error: %1")
                             .arg(m_qsltablemodel->lastError().text()));
    }
    m_qsltablemodel->setSort(0,Qt::AscendingOrder);
}

void QWidgetDlPara1::revert()
{
    m_qsltablemodel->revertAll();
}


